<?php
class StuscholarshipController extends Controller{
	     
		  public $isActivated = 1;
          public function accessRules()
		 {
			return array(
				array(
					'allow',  // allow all users to access 'index' and 'view' actions.
					'expression'=>'$user->rights == 1',
				),
				array(
					'deny',  // deny all users
					'users'=>array('*'),
					'message'=>'You do not have access to this page ! ! ',
				),
			);
		 } 
		 public function actionIndex(){
			  $this->breadcrumbs=array(
               '综合奖学金'=>array('index'),
               '添加',
             );
			 $pd = Scholaritem::model()->findAll("module = '品德行为'");
			 $xy = Scholaritem::model()->findAll("module = '学业表现'");
			 $wt = Scholaritem::model()->findAll("module = '文体综合素质'");
			 
			 $this->render("index",array('pd'=>$pd,'xy'=>$xy,'wt'=>$wt));
		 }
		 public function actionAddinte(){
			if(Yii::app()->request->requestType == 'POST'){
				if($_POST['term'] == ''){
				  Yii::app()->user->setFlash('add','请添加学期');	
			   }else{
				 $stusc = new Stuscholarship;
				 $stusc->uId = Yii::app()->user->getId();
				 $stusc->module = '学业表现';
				 $stusc->term = $_POST['term'];
				 $stusc->detail = $_POST['detail'];
				 $stusc->credit = $_POST['credit'];
				 $stusc->addtime = date('Y-m-d H:i:s');
				  $stusc->condition = 1; //1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				 if($stusc->save()){
					Yii::app()->user->setFlash('add','添加成功，请到已添加的综合奖学金查看，谢谢');
				 }else{
					 
					 $this->logAr($stusc);
					 Yii::app()->user->setFlash('add','添加失败！');
				 }
				  
			   }
				$this->redirect(array('stuscholarship/index'));
			}
			 
		 }
		 public function actionAddmor(){
			 if(Yii::app()->request->requestType == 'POST'){
				if($_POST['term'] == ''){
				  Yii::app()->user->setFlash('add','请添加学期');	
			   }else{
				 $stusc = new Stuscholarship;
				 $stusc->uId = Yii::app()->user->getId();
				 $stusc->module = '品德行为';
				 $stusc->term = $_POST['term'];
				 $stusc->detail = $_POST['detail'];
				 $stusc->credit = $_POST['credit'];
				 $stusc->addtime = date('Y-m-d H:i:s');
				  $stusc->condition = 1; //1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				 if($stusc->save()){
					 
					 Yii::app()->user->setFlash('add','添加成功，请到已添加的综合奖学金查看，谢谢');
				 }else{
					 
					 $this->logAr($stusc);
					 Yii::app()->user->setFlash('add','添加失败！');
				 }
				  
			   }
				$this->redirect(array('stuscholarship/index'));
			}
			 
		 }
		 public function actionAddphy(){
			if(Yii::app()->request->requestType == 'POST'){
				if($_POST['term'] == ''){
				  Yii::app()->user->setFlash('add','请添加学期');	
			   }else{
				 $stusc = new Stuscholarship;
				 $stusc->uId = Yii::app()->user->getId();
				 $stusc->module = '文体综合素质';
				 $stusc->term = $_POST['term'];
				 $stusc->detail = $_POST['detail'];
				 $stusc->credit = $_POST['credit'];
				 $stusc->addtime = date('Y-m-d H:i:s');
				 $stusc->condition = 1; //1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				 if($stusc->save()){
					 
					 Yii::app()->user->setFlash('add','添加成功，请到已添加的综合奖学金查看，谢谢');
				 }else{
					 
					 $this->logAr($stusc);
					 Yii::app()->user->setFlash('add','添加失败！');
				 }
				  
			   }
				$this->redirect(array('stuscholarship/index'));
			} 
			 
		 }
		 public function actionMyscholarship(){
			 $this->breadcrumbs=array(
               '综合奖学金'=>array('index'),
               '管理我的综合奖学金',
             );
			 $ci = new CDbCriteria;
			 $ci->order = 'term';
			 $ci->addCondition('uId = '.Yii::app()->user->getId());
			 $stuscs = Stuscholarship::model()->findAll($ci);
			 $t1 = array();$credit1 = array('inte'=>0,'mor'=>0,'phy'=>0,'performance'=>0);
			 $t2 = array();$credit2 = array('inte'=>0,'mor'=>0,'phy'=>0,'performance'=>0);
			 $t3 = array();$credit3 = array('inte'=>0,'mor'=>0,'phy'=>0,'performance'=>0);
			 $t4 = array();$credit4 = array('inte'=>0,'mor'=>0,'phy'=>0,'performance'=>0);
			 $t5 = array();$credit5 = array('inte'=>0,'mor'=>0,'phy'=>0,'performance'=>0);
			 $t6 = array();$credit6 = array('inte'=>0,'mor'=>0,'phy'=>0,'performance'=>0);
			 $t7 = array();$credit7 = array('inte'=>0,'mor'=>0,'phy'=>0,'performance'=>0);
			 $t8 = array();$credit8 = array('inte'=>0,'mor'=>0,'phy'=>0,'performance'=>0);
			
			foreach($stuscs as $sc){
				
				switch($sc->term){
                 case '第一学期':$t = &$t1;$c = &$credit1;break;				
                 case '第二学期':$t = &$t2;$c = &$credit2;break;				
                 case '第三学期':$t = &$t3;$c = &$credit3;break;				
                 case '第四学期':$t = &$t4;$c = &$credit4;break;				
                 case '第五学期':$t = &$t5;$c = &$credit5;break;				
                 case '第六学期':$t = &$t6;$c = &$credit6;break;				
                 case '第七学期':$t = &$t7;$c = &$credit7;break;				
                 case '第八学期':$t = &$t8;$c = &$credit8;break;				
				}
				$t[] = $sc;
				if($sc->condition == '3'){
					  if($sc->module == '品德行为')
							$c['mor']+= $sc->credit;
					  else if($sc->module == '学业表现')
							$c['inte']+= $sc->credit;
					   else if($sc->module == '文体综合素质')
							$c['phy']+= $sc->credit;
					}
			 }
			 $schPer = Scholarshippercent::model()->find();
			 if(!$schPer)
				 $schPer = new Scholarshippercent;
			 $stuPerformance = Stuperformance::model()->find('uId = :u',array(':u'=>Yii::app()->user->getId()));
			 if(!$stuPerformance)
				 $stuPerformance = new Stuperformance;
			 $i = 1;
				 $credit1['per'] = $stuPerformance->term_1;
				 $credit1['inte'] = $credit1['inte']*$schPer->integrate/100.0 + $credit1['per']*$schPer->performance/100.0;
				 $credit1['phy'] = $credit1['phy']*$schPer->physical/100.0;
				 $credit1['mor'] = $credit1['mor']*$schPer->morality/100.0;
				 
				 $credit2['per'] = $stuPerformance->term_2;
				 $credit2['inte'] = $credit2['inte']*$schPer->integrate/100.0 + $credit2['per']*$schPer->performance/100.0;
				 $credit2['phy'] = $credit2['phy']*$schPer->physical/100.0;
				 $credit2['mor'] = $credit2['mor']*$schPer->morality/100.0;
				 
				 $credit3['per'] = $stuPerformance->term_3;
				 $credit3['inte'] = $credit3['inte']*$schPer->integrate/100.0 + $credit3['per']*$schPer->performance/100.0;
				 $credit3['phy'] = $credit3['phy']*$schPer->physical/100.0;
				 $credit3['mor'] = $credit3['mor']*$schPer->morality/100.0;
				 
				 $credit4['per'] = $stuPerformance->term_4;
				 $credit4['inte'] = $credit4['inte']*$schPer->integrate/100.0 + $credit4['per']*$schPer->performance/100.0;
				 $credit4['phy'] = $credit4['phy']*$schPer->physical/100.0;
				 $credit4['mor'] = $credit4['mor']*$schPer->morality/100.0;
				 
				 $credit5['per'] = $stuPerformance->term_5;
				 $credit5['inte'] = $credit5['inte']*$schPer->integrate/100.0 + $credit5['per']*$schPer->performance/100.0;
				 $credit5['phy'] = $credit5['phy']*$schPer->physical/100.0;
				 $credit5['mor'] = $credit5['mor']*$schPer->morality/100.0;
                 
				 $credit6['per'] = $stuPerformance->term_6;
				 $credit6['inte'] = $credit6['inte']*$schPer->integrate/100.0 + $credit6['per']*$schPer->performance/100.0;
				 $credit6['phy'] = $credit6['phy']*$schPer->physical/100.0;
				 $credit6['mor'] = $credit6['mor']*$schPer->morality/100.0;
				 
				 $credit7['per'] = $stuPerformance->term_7;
				 $credit7['inte'] = $credit7['inte']*$schPer->integrate/100.0 + $credit7['per']*$schPer->performance/100.0;
				 $credit7['phy'] = $credit7['phy']*$schPer->physical/100.0;
				 $credit7['mor'] = $credit7['mor']*$schPer->morality/100.0;
				 
				 $credit8['per'] = $stuPerformance->term_8;
				 $credit8['inte'] = $credit8['inte']*$schPer->integrate/100.0 + $credit8['per']*$schPer->performance/100.0;
				 $credit8['phy'] = $credit8['phy']*$schPer->physical/100.0;
				 $credit8['mor'] = $credit8['mor']*$schPer->morality/100.0;
				 
			 $this->render("myscholarship",array('t1'=>$t1,'t2'=>$t2,'t3'=>$t3,'t4'=>$t4,'t5'=>$t5,'t6'=>$t6,'t7'=>$t7,'t8'=>$t8,'credit1'=>$credit1,'credit2'=>$credit2,'credit3'=>$credit3,'credit4'=>$credit4,'credit5'=>$credit5,'credit6'=>$credit6,'credit7'=>$credit7,'credit8'=>$credit8));
			}
			 
		public function actionDelete(){
			if(Yii::app()->request->isAjaxRequest){
				$id = $_POST['stuscid'];
				$sc = Stuscholarship::model()->findByPk($id);
				if($sc){
					if($sc->condition != 3){
						if($sc->delete()){
							echo json_encode(array('code'=>200,'msg'=>'删除成功'));
						}else{
							echo json_encode(array('code'=>505,'msg'=>'删除失败'));
						}
					}else{
						
						echo json_encode(array('code'=>306,'msg'=>'不能删除'));
					}
				}else{
					echo json_encode(array('code'=>404,'msg'=>'没有找到这个条目'));
				}
				
			}else{
				echo json_encode(array('code'=>504,'msg'=>'No service'));
				
			}
			
		}	 
		 
		 
		 
}